package ooday04;

import java.util.concurrent.Callable;

//向上造型，超类型的引用指向派生类对象，A自动类型转换（由小到大）
public class PolymorphicDemo {
    public static void main(String[] args) {
        Dog o1=new Dog("小黑",2,"黄");
        //o1.lookhome/age/name/color/eat/swim
        Animal o2= new Dog("小黄",2,"黑");
        //只能调用Animal里的属性和方法
        Swim o3=new Dog("小白",2,"白");
        //只能调用Swim里的
        Master master =new Master();
        Dog dog =new Dog("小黑",2,"黑");
      Chick chick=new Chick("小黄",2,"黄");
      Fish fish=new Fish("鱼",2,"白");
      master.feed(dog);
      master.feed(fish);
      master.feed(chick);
      //演示向上造型（多态）的第二个应用
Animal []animals= new Animal[5];
animals [0]=new Dog("小黑",1,"黑");
animals [1]= new Dog("小白",1,"白");
animals [2]=new Chick("火鸡",1,"红");
animals[3]=new Fish("草鱼",1,"银");
        animals [4]=new Fish("鲤鱼",1,"红");
        for(int i=0;i<animals.length;i++){
            System.out.println(animals[i].name);
            animals[i].eat();
        }
        //演示强转成功的条件
Animal o=new Dog("小黑",1,"黑");
        Dog g=(Dog) o;
        Swim s=(Swim) o;
    System.out.println(o instanceof Dog);
    System.out.println(o instanceof Swim);
    System.out.println(o instanceof Fish);
    if (o instanceof Fish){
        Fish f=(Fish) o;
    }else {
        System.out.println("o不是Fish类型");

    }
    for (int j=0;j<animals.length;j++){
        System.out.println(animals[j].name);
        if (animals[j] instanceof Dog ){
            Dog dogs=(Dog) animals[j];
            dogs.lookhome();
        }
        if (animals[j] instanceof Fish){
            Fish fishs=(Fish) animals[j];
            fishs.eat();
        }
        if (animals[j] instanceof Swim){
            Swim swims=(Swim) animals[j];
            swims.swim();
        }
    }
        };
    }

